package com.tg.data.bean;

import android.text.TextUtils;

import androidx.annotation.NonNull;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DeviceCapAIs {
    private final List<DeviceCapAI> list = new ArrayList<>();

    public DeviceCapAIs(@NonNull String src) {
        try {
            HashMap<String, JSONArray> hashMap = JSON.parseObject(src, HashMap.class);
            if (hashMap != null) {
                JSONArray hw = hashMap.get("hw_ai");
                List aiList = JSON.parseArray(hw.toJSONString(), DeviceCapAI.class);
                if (aiList != null) {
                    list.addAll(aiList);
                }
            }
        } catch (Exception e) {

        }

    }

    public boolean isSupportBodyDetectionFrame() {
        DeviceCapAI ai = getByType("bd");
        if (ai != null && ai.flags >> 1 == 1) {
            return true;
        }
        return false;
    }

    public boolean isSupportTrackingCloseup() {
        DeviceCapAI ai = getByType("bt");
        if (ai != null) {
            return true;
        }
        return false;
    }

    public boolean isSupportTumble(){
        DeviceCapAI ai = getByType("tumble");
        return ai != null;
    }
    
    public boolean isSupportSittingPosition() {
        DeviceCapAI ai = getByType("bad_posture");
        return ai != null;
    }

    public DeviceCapAI getByType(String type) {

        for (DeviceCapAI ai :
                list) {
            if (TextUtils.equals(ai.type, type)) {
                return ai;
            }
        }

        return null;
    }

    static class DeviceCapAI {
        public String type;
        public int flags;

        public DeviceCapAI() {

        }

    }
}
